package Exercise;
//根据id查找用户是否存在，存在则返回索引，不存在则返回-1

import java.util.ArrayList;
class User{
    private String name;  // 用户名
    private String id;  //用户id
    private String password;  // 用户密码

    public User() {
    }

    public User(String name, String id, String password) {
        this.name = name;
        this.id = id;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String toString(){
        return "用户名："+this.name +" id:"+ this.id +" 密码："+this.password;
    }

}
public class Set_1 {
    public static void main(String[] args) {
        //创建容器和用户对象
        ArrayList<User> list = new ArrayList<>();
        User u1 = new User("张三", "u1234","12345");
        User u2 = new User("李四", "i1234","123456");
        User u3 = new User("王五", "p1234","12345pwe");
        //添加对象
        list.add(u1);
        list.add(u2);
        list.add(u3);
        //调用方法
        String id = "i1234";
        int suoyin = Decide(id, list);
        if(suoyin!=-1){
            System.out.println("用户存在，索引为"+suoyin+";对应的信息为:");
            System.out.println(list.get(suoyin).toString());
        }
        else
            System.out.println("用户不存在！");
    }

    //判断是否存在，并返回相应的结果
    public static int Decide(String id, ArrayList<User> list){
        for (int i = 0; i < list.size(); i++) {
           if(id.equals(list.get(i).getId()))
               return i;
        }
        return -1;
    }

}
